package lumis.portal.presentation.tag.page;

import lumis.portal.presentation.core.ILayoutFileComponentFactory;
import lumis.portal.presentation.core.LayoutFileApplication;
import lumis.portal.presentation.core.LayoutFileTag;

import org.apache.wicket.Component;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.MarkupStream;

/**
 * Represents built-in tags of the layout file 'page' namespace.
 * 
 * @see PageMarkupFilter
 *
 * @version $Revision: 13093 $ $Date: 2011-05-28 18:40:18 -0300 (Sat, 28 May 2011) $
 * @since 6.0.0
 */
public class LayoutFilePageTag extends LayoutFileTag
{
	private boolean dummy;

	/**
	 * Creates a new LayoutFilePageTag.
	 * @param tag the tag to be used as base.
	 * @param factory the component factory.
	 * @since 6.0.0
	 */
	public LayoutFilePageTag(ComponentTag tag, ILayoutFileComponentFactory factory)
	{
		super(tag, factory);
	}
	
	/**
	 * Creates a new LayoutFilePageTag based on a {@link LayoutFileTag}.
	 * @param tag the LayoutFileTag.
	 * @param dummy the value for dummy property.
	 * @since 6.0.0
	 */
	protected LayoutFilePageTag(LayoutFileTag tag, boolean dummy)
	{
		this(tag, tag.getComponentFactory());
		setDummy(dummy);
	}

	/**
	 * Creates a new LayoutFilePageTag based on another LayoutFilePageTag.
	 * @param tag the LayoutFilePageTag.
	 * @since 6.0.0
	 */
	protected LayoutFilePageTag(LayoutFilePageTag tag)
	{
		this(tag, tag.dummy);
	}

	/**
	 * Sets whether this is a dummy tag.
	 * <p> 
	 * Dummy tags should have only their body rendered, since they are intended
	 * to be used as a place-holder for a HTML fragment with no root element.
	 * If this tag is dummy and {@link LayoutFileApplication#getStripLayoutFileTags()}
	 * is <code>true</code>, this tag will call {@link Component#setRenderBodyOnly(boolean)}
	 * with <code>true</code> during {@link #onBeforeRender(Component, MarkupStream)}.
	 * @param dummy whether this is a dummy tag.
	 * @since 6.0.0
	 */
	protected void setDummy(boolean dummy)
	{
		this.dummy = dummy;
	}
	
	@Override
	public void onBeforeRender(Component component, MarkupStream markupStream)
	{
		super.onBeforeRender(component, markupStream);
		
		if (dummy && ((LayoutFileApplication)component.getApplication()).getStripLayoutFileTags())
		{
			component.setRenderBodyOnly(true);
		}
	}

	@Override
	public LayoutFilePageTag mutable()
	{
		if (xmlTag.isMutable())
			return this;
		else
			return new LayoutFilePageTag(super.mutable(), dummy);
	}	
}
